<?php

/**
* ScoreBoardModule.class.php
*
* Module responsable for code hunt
*
* @author     Blazej Zieba <b@zieba.info>
* @copyright  2007 konekto.com
* @version    CVS: $Id:$
*/
require_once("Auth/Auth.php");
require_once("framework/config.php");
require_once("UserAuth.php");
require_once("framework/PageModule.class.php");
require_once("HTML/QuickForm.php");
require_once("HTML/QuickForm/Renderer/Default.php");
require_once("HTML/Template/IT.php");


// {{{ AuthForm

class ScoreBoardModule extends PageModule 
{
    // {{{ getContent()

    
    /**
    * getContent()
    *
    */

    function getContent()
    {

        trigger_error("ENTER ScoreBoardModule::getContent()", E_USER_NOTICE);

        $options = &PEAR::getStaticProperty('CONFIG_INI','TEMPLATES');        
        $tpl = new HTML_Template_IT($options["templates_dir"]);
        $tpl->loadTemplatefile("score_board.tpl.xhtml",true,true);

        $statusTpl = new HTML_Template_IT($options["templates_dir"]);
        $statusTpl->loadTemplatefile("status.tpl.xhtml",FALSE,FALSE);

        $tpl->setCurrentBlock('row');
        $tpl->setVariable("POS",_('L.P.'));
        $tpl->setVariable("USER",_('Gracz'));
        $tpl->setVariable("NUMOFCODE",_('Uplowane kody'));
        $tpl->setVariable("HOUR",_('Łączny czas godz'));
        $tpl->setVariable("MIN",'min');
        $tpl->setVariable("SEC",'sec');
        $tpl->parseCurrentBlock();

        $options = &PEAR::getStaticProperty('CONFIG_INI','DSN'); 
        $dbh = new PDO($options['dsn_string']);
        $dbh->setAttribute( PDO::ATTR_ERRMODE, PDO::ERRMODE_EXCEPTION );

        $global = &PEAR::getStaticProperty('CONFIG_INI','GLOBAL'); 

        $startTime = strtotime($global['start_time']);

        if ( $startTime === false )
        {
            trigger_error("ScoreBoardModule::getContent()  Fatal error invalid start time. ".$global['start_time'], E_USER_ERROR);
        }

        $sth = $dbh->prepare("SELECT hunted_code.owner AS owner,COUNT(hunted_code.code_id) AS num_of_code, MAX(hunted_code.time) - $startTime as duration FROM hunted_code,guide WHERE guide.rowid = hunted_code.code_id  GROUP BY hunted_code.owner ORDER BY num_of_code DESC, duration ASC;");
        $sth->execute();
        $result = $sth->fetchAll(PDO::FETCH_ASSOC);
        
        foreach( $result as $key => $value)
        {
            $tpl->setCurrentBlock('row');
            $tpl->setVariable("POS",$key+1);
            $tpl->setVariable("USER",$value['owner']);
            $tpl->setVariable("NUMOFCODE",$value['num_of_code']);
            $tpl->setVariable("HOUR", (intval(intval($value['duration']) / 3600)) );
            $tpl->setVariable("MIN",(intval(($value['duration'] / 60) % 60)) );
            $tpl->setVariable("SEC", (intval($value['duration'] % 60) ));

            $tpl->parseCurrentBlock();   
        }
        
        $statusTpl->setVariable("STATUS",_("Tablica wyników"));
        return $statusTpl->get().$tpl->get().$statusTpl->get();
    }
    
    // }}}

}

// }}} 

?>

